// 简单封装store
const localStore = {
  set (key, value) {
    if (typeof value === 'object') {
      try {
        localStorage.setItem(key, JSON.stringify(value))
      } catch (err) {
        localStorage.setItem(key, value)
      }
    } else {
      localStorage.setItem(key, value)
    }
  },
  get (key) {
    let value = localStorage.getItem(key)

    try {
      if (value) {
        value = JSON.parse(value)
      }

      return value
    } catch (err) {
      return value
    }
  },
  remove (key) {
    localStorage.removeItem(key)
  },
  clear () {
    localStorage.clear()
  }
}

const sessionStore = {
  set (key, value) {
    if (typeof value === 'object') {
      try {
        sessionStorage.setItem(key, JSON.stringify(value))
      } catch (err) {
        sessionStorage.setItem(key, value)
      }
    } else {
      sessionStorage.setItem(key, value)
    }
  },
  get (key) {
    let value = sessionStorage.getItem(key)

    try {
      if (value) {
        value = JSON.parse(value)
      }

      return value
    } catch (err) {
      return value
    }
  },
  remove (key) {
    sessionStorage.removeItem(key)
  },
  clear () {
    sessionStorage.clear()
  }
}

export {
  localStore,
  sessionStore
}
